package com.moleq.util;

import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jp.co.casio.caios.framework.device.lineprintertools.Encoding;
import android.content.Context;

import com.moleq.enums.PaymentType;
import com.moleq.enums.TransactionType;
import com.moleq.logic.MoleQConstants;
import com.moleq.logic.TransactionManager;
import com.moleq.models.CustomerSetting;
import com.moleq.models.Departmant;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SubDepartmant;
import com.moleq.service.ServiceManager;

public class MoleQUtil {
	
	private final static char[] ESC_BASE_CH	= {0x1B, '|'};
	private final static char[] LINEFEED_CH = {'\n'};
	private final static char ESC_CANCEL = '!';

	private final static String ESC_BASE		= CharBuffer.wrap(ESC_BASE_CH).toString();
	private final static String PAPER_CUT		= "P";
	private final static String PAPER_FEED_CUT	= "fP";	
	private final static String BITMAP			= "B";
	private final static String TOP_LOGO		= "tL";
	private final static String BOTTOM_LOGO		= "bL";
	private final static String MULTI_LINE_FEED	= "lF";
	private final static String ONE_LINE_FEED	= CharBuffer.wrap(LINEFEED_CH).toString();	
	private final static String BARCODE			= "R";
	private final static String SINGLE_SIZE		= "1C";
	private final static String DOUBLE_SIZE		= "2C";	
	private final static String DOUBLE_HEIGHT	= "3C";	
	private final static String DOUBLE_WIDTH_HEIGHT	= "4C";	

	public static String fixNumber(double num) {
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
		numberFormat.setMinimumFractionDigits(2);
		numberFormat.setMaximumFractionDigits(2);
		return numberFormat.format(num);
	}
	
	public static String fixNumberWithDigits(double num) {
		String result = String.valueOf(num);
		int i = (int) num;
		
		result =new DecimalFormat("###0.00##").format(num);
		
		int idx = result.indexOf(".")+1;
		
		String temp = result.substring(idx,result.length());
		
		int j = temp.length();
		while (j<4) {
			
			result +=" ";
			j++;
		}
		
		return result;
	}
//	public static String customFormat(String pattern, float value) {
//		DecimalFormat myFormatter = new DecimalFormat(pattern);
//		String output = myFormatter.format(value);
//		return output;
//	}
	
	public static String fixNumber2(double num){
		//return new DecimalFormat("#,##0.###").format(num);
		String result = String.valueOf(num);
		int i = (int)num;
		if(num == i){
			result = new DecimalFormat("#,##0").format(num);
		}else{
			result = fixNumber(num);
		}
		
		return result;

	}
	
	public static String fixWeight(double num,int unit){
		//return new DecimalFormat("#,##0.###").format(num);
		String result = String.valueOf(num);
		int i = (int)num;
		if(num == i){
			result = new DecimalFormat("#,##0.00").format(num);
		}else{
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setMinimumFractionDigits(unit);
			numberFormat.setMaximumFractionDigits(unit);
			result = numberFormat.format(num);
		}

		return result;

	}
	
	public static String getCurrentDate(){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyyMMdd");     
		Date   curDate   =   new   Date(System.currentTimeMillis());//閿熸枻鎷峰彇閿熸枻鎷峰墠鏃堕敓鏂ゆ嫹     
		String   str   =   formatter.format(curDate);   
		  
		return str;
	}
	
	public static String getCurrentTime(){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("hhmmss");     
		Date   curTime   =   new   Date(System.currentTimeMillis());//閿熸枻鎷峰彇閿熸枻鎷峰墠鏃堕敓鏂ゆ嫹     
		String   str   =   formatter.format(curTime);   
		  
		return str;
	}
	
	public static String getLocalTime(){
		SimpleDateFormat formatter = new SimpleDateFormat ("HHmmss");  
		String str = formatter.format(new Date());   
		  
		return str;
	}
	
	public static String getDateToString(String format,Date date){
		SimpleDateFormat formatter = new SimpleDateFormat (format);  
		String str = formatter.format(date);   
		  
		return str;
	}
	
	public static String customFormat(String pattern, double value ) { 
		  DecimalFormat myFormatter = new DecimalFormat(pattern); 
		  String output = myFormatter.format(value); 
//		  System.out.println(value + " " + pattern + " " + output); 
		  return output;
		} 
	
	public static String localizedFormat(String pattern, double value, Locale loc ) { 
		  NumberFormat nf = NumberFormat.getNumberInstance(loc); 
		  DecimalFormat df = (DecimalFormat)nf; 
		  df.applyPattern(pattern); 
		  String output = df.format(value); 
//		  System.out.println(pattern + " " + output + " " + loc.toString()); 
		  return output;
	} 


	// public static boolean isNumeric(String str) {
	// for (int i = str.length(); --i >= 0;) {
	// if (!Character.isDigit(str.charAt(i))) {
	// return false;
	// }
	// }
	//
	// return true;
	// }

	// =========================================================
	// Get Encoded String
	// =========================================================
	public static byte[] getEncodedString(String str, String sLang) {

		String e_str, charEncode = "ISO-8859-1";

		if (sLang.equals(Locale.JAPANESE.getLanguage())) {
			charEncode = "MS932";
			e_str = str;
		} else if (sLang.equals(Locale.SIMPLIFIED_CHINESE.getLanguage())) {
			charEncode = "GB2312";
			e_str = str;
		} else if (sLang.equals(Locale.TRADITIONAL_CHINESE.getLanguage())) {
			charEncode = "BIG5";
			e_str = str;
		} else
			e_str = Encoding.UTF8toPC858(str); // Encode UTF-8 -> PC858 except

		// Normalize the display string to character byte array.
		StringBuffer sbuffer = new StringBuffer();
		sbuffer.append(e_str);

		byte[] ldisplay = new byte[20];
		byte[] ls1 = null;

		try {
			ls1 = e_str.getBytes(charEncode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < ldisplay.length; i++) {
			if (i < ls1.length)
				ldisplay[i] = ls1[i];
			else
				ldisplay[i] = ' ';
		}

		return ldisplay;
	}

	// 浣块敓鏂ゆ嫹java閿熺殕杈炬嫹閿熸枻鎷�
//	public static boolean isNumeric(String str) {
//		for (int i = str.length(); --i >= 0;) {
//			if (!Character.isDigit(str.charAt(i))) {
//				return false;
//			}
//		}
//		return true;
//	}
	//
	 //浣块敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰紡
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	public static boolean isNumeric2(String str) {  
		if (str.trim().equals("") || str.trim().equals(".")) {
			return false;
		}
		Pattern pattern = Pattern.compile("-?[0-9]*.?[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true; 
    }
	
	// //浣块敓鏂ゆ嫹ascii閿熸枻鎷�
	// public static boolean isNumeric(String str){
	// for(int i=str.length();--i>=0;){
	// int chr=str.charAt(i);
	// if(chr<48 || chr>57)
	// return false;
	// }
	// return true;
	// }
	 
	 /*  
	   * 閿熷彨璁规嫹閿熻鍑ゆ嫹涓洪敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓绲沷uble閿熸枻鎷穎loat  
	  * @param str 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰潃閿燂拷 
	   * @return 閿熻闈╂嫹閿熸枻鎷烽敓鏂ゆ嫹閿熺但rue,閿熸枻鎷烽敓娲ヨ繑浼欐嫹false  
	*/    
	   public static boolean isDouble(String str) {    
	     Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");    
	     return pattern.matcher(str).matches();    
	   }  
	   
	   /*  
		   * 鍙栭敓鏂ゆ嫹鍓嶆椂閿熸枻鎷�
		   * @return 閿熸枻鎷烽敓鎴鎷峰墠鏃堕敓鏂ゆ嫹  
		*/ 
	   public static String getNow(){
		   
		   SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyyMMddHHmm");     
		   Date   curDate   =   new   Date(System.currentTimeMillis());//閿熸枻鎷峰彇閿熸枻鎷峰墠鏃堕敓鏂ゆ嫹     
		  String   str   =   formatter.format(curDate);   
		  
		  return str;
	   }
	   public static String getNow2(){
		   
		   SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyyMMddHHmmss");     
		   Date   curDate   =   new   Date(System.currentTimeMillis());//閿熸枻鎷峰彇閿熸枻鎷峰墠鏃堕敓鏂ゆ嫹     
		  String   str   =   formatter.format(curDate);   
		  
		  return str;
	   }
	   
	   public static String getNowwithFormat(String format){
		   
		   SimpleDateFormat   formatter   =   new   SimpleDateFormat   (format);     
		   Date   curDate   =   new   Date(System.currentTimeMillis());//閿熸枻鎷峰彇閿熸枻鎷峰墠鏃堕敓鏂ゆ嫹     
		  String   str   =   formatter.format(curDate);   
		  
		  return str;
	   }
	   public static String repeatString(String s, int n) {
			// TODO Auto-generated method stub
			if (n <= 0) return"";
			return String.format(String.format("%%0%dd",n), 0).replace("0", s);
		}
	  
	   public static String fixNumber3(int no,String format){
		   DecimalFormat   df   =   new   DecimalFormat(format); 
		   String str = df.format(no);
		   return str;
	   }
	   public static String fixNumber4(double no,String format){
		   DecimalFormat   df   =   new   DecimalFormat(format); 
		   String str = df.format(no);
		   return str;
	   }
	   
	   public static int getYear(String birthday){
		   
		   int iage = 0;  
		   
	        if (birthday != "" || birthday != null) {  
	            int year = Integer.parseInt(birthday.substring(0, 4));  
	            int month = Integer.parseInt(birthday.substring(5, 7));  
	            int day = Integer.parseInt(birthday.substring(7, 9));  
	  
	            Calendar birthDate = new GregorianCalendar(year, month, day);  
	            Calendar today = Calendar.getInstance();  
	  
	            if (today.get(Calendar.YEAR) > birthDate.get(Calendar.YEAR)) {  
	                iage = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR)  
	                        - 1;  
	                if (today.get(Calendar.MONTH) + 1 > birthDate  
	                        .get(Calendar.MONTH)) {  
	                    iage++;  
	                } else if (today.get(Calendar.MONTH) + 1 == birthDate  
	                        .get(Calendar.MONTH)) {  
	                    if (today.get(Calendar.DAY_OF_MONTH) > birthDate  
	                            .get(Calendar.DAY_OF_MONTH)) {  
	                        iage++;  
	                    }  
	                }  
	            }  
	            return iage;  
	        }  
	        return 0;   
	   }
	   
	public static int getYear2(String birthday) {
		int iage = 0;
		if (birthday.equals("null")|| birthday.equals("") ||birthday == null) {
			iage = 0;
		} else {

			int year = Integer.parseInt(birthday.substring(0, 4));
			int month = Integer.parseInt(birthday.substring(4, 6));
			int day = Integer.parseInt(birthday.substring(6, 8));

			Calendar birthDate = new GregorianCalendar(year, month, day);
			Calendar today = Calendar.getInstance();
			
			if (today.get(Calendar.YEAR) > birthDate.get(Calendar.YEAR)) {
				iage = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR) - 1;
				if (today.get(Calendar.MONTH) + 1 > birthDate.get(Calendar.MONTH)) {
					iage++;
				} else if (today.get(Calendar.MONTH) + 1 == birthDate
						.get(Calendar.MONTH)) {
					if (today.get(Calendar.DAY_OF_MONTH) > birthDate
							.get(Calendar.DAY_OF_MONTH)) {
						iage++;
					}
				}
			}

		}

		return iage;
	}
	   
	   public static String fixDateTime(String date) {

			Date curDate = null;
			String str;
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
			DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			// Date curDate =(Date)date;
			try {

				curDate = format1.parse(date);

			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (curDate == null) {
				str = "";
			} else {
				str = formatter.format(curDate);
			}
			return str;
		};
		
		public static Date ChangeDate(String Date){
			Date curDate = null;
			DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				curDate = format1.parse(Date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return curDate;
		}
		
		public static Date ChangeDate(String Date,String format){
			Date curDate = null;
			DateFormat format1 = new SimpleDateFormat(format);
			try {
				curDate = format1.parse(Date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return curDate;
		}
		
		/** 
		 * 获取两个日期之间的间隔天数 
		 * @return 
		 */  
		public static int getGapCount(String startDate, String endDate) { 
			
		       Calendar fromCalendar = Calendar.getInstance();    
		       fromCalendar.setTime(ChangeDate(startDate));    
		       fromCalendar.set(Calendar.HOUR_OF_DAY, 0);    
		       fromCalendar.set(Calendar.MINUTE, 0);    
		       fromCalendar.set(Calendar.SECOND, 0);    
		       fromCalendar.set(Calendar.MILLISECOND, 0);    
		   
		       Calendar toCalendar = Calendar.getInstance();    
		       toCalendar.setTime(ChangeDate(endDate));    
		       toCalendar.set(Calendar.HOUR_OF_DAY, 0);    
		       toCalendar.set(Calendar.MINUTE, 0);    
		       toCalendar.set(Calendar.SECOND, 0);    
		       toCalendar.set(Calendar.MILLISECOND, 0);    
		   
		       return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));  
		} 
	    
		public static String fixDateTime2(String date,String outdateformatter,String indateformatter) {

			Date curDate = null;
			String str;
			SimpleDateFormat formatter = new SimpleDateFormat(outdateformatter);
			DateFormat format1 = new SimpleDateFormat(indateformatter);
			// Date curDate =(Date)date;
			try {

				curDate = format1.parse(date);

			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (curDate == null) {
				str = "";
			} else {
				str = formatter.format(curDate);
			}
			return str;
		};
		
		public static String getNowPreDate(){
			
			Calendar ca = Calendar.getInstance();//閿熺煫纰夋嫹涓�敓鏂ゆ嫹Calendar閿熸枻鎷峰疄閿熸枻鎷�   
	        ca.add(Calendar.DATE, -1); //閿熼摪浠界》鎷�     
	        Date lastdate = ca.getTime();
	        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");  
	        return  sf.format(lastdate);  
		}
		
		public static int getBetweenDays(String day1,String day2){
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		    long to;
		    long from;
		    int day = 0;
			try {
				to = df.parse(day1).getTime();
				from = df.parse(day2).getTime();
				day = BigDecmialTool.convertsToInt((to - from) / (1000 * 60 * 60 * 24));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		   
		    return day;
		}


		public static String parsePrintContent(String printText){
			String result = printText;
			
			ArrayList<String> removalStrLst = new ArrayList<String>();
			removalStrLst.add(ESC_BASE);
			removalStrLst.add(SINGLE_SIZE);
			removalStrLst.add(DOUBLE_HEIGHT);
			removalStrLst.add(DOUBLE_SIZE);
			
			for(int i = 0; i<removalStrLst.size();i++){
				result = result.replace(removalStrLst.get(i), "");
			}
			
						
			return result;
		}
		
		public static String fixPrintPrice(double num) {
			String result = "";
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setMinimumFractionDigits(2);
			numberFormat.setMaximumFractionDigits(2);
			
			if (GlobalInfo.getInstance().isDemoVersion) {
				if(num< 0){
					result = numberFormat.format( Math.abs(num));
					result = result.substring(0,result.length() - 2) + "XX";
					result = result +"-";
				}else{
					result =  numberFormat.format(num);
					result = result.substring(0,result.length() - 2) + "XX ";
				}
			}else {
				if(num< 0){
					result = numberFormat.format( Math.abs(num));
					result = result +"-";
				}else{
					result =  numberFormat.format(num) +" ";
				}
			}
			return GlobalInfo.getInstance().CurrencySign + result;
		}
		
//		public static String fixPrintPriceXX(double num) {
//			String result = "";
//			NumberFormat numberFormat = NumberFormat.getNumberInstance();
//			numberFormat.setMinimumFractionDigits(2);
//			numberFormat.setMaximumFractionDigits(2);
//			
//			if(num< 0){
//				result = numberFormat.format( Math.abs(num));
//				result = result.substring(0,result.length() - 2) + "XX";
//				result = result +"-";
//			}else{
//				result =  numberFormat.format(num);
//				result = result.substring(0,result.length() - 2) + "XX ";
//			}
//			
//			return GlobalInfo.getInstance().CurrencySign + result;
//		}
		
		public static String fixPrintQty(String qty) {
			String result = qty;
		
			
			if(qty.length() > 0 && !qty.trim().equals("")){
//				result = qty.substring(1,qty.length() - 1);
				result =  fixNumber2(Double.valueOf(result));
				//result = Integer.valueOf(result).toString();
			}

			return result;
		}
		
		public static String fixPrintPrice(double num,boolean showFoodstamp,boolean showTax) {
			String result = "";
			String fsTag = " ";
			String taxTag = " ";
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setMinimumFractionDigits(2);
			numberFormat.setMaximumFractionDigits(2);
			
			if (GlobalInfo.getInstance().isDemoVersion) {
				if(num< 0){
					result = numberFormat.format( Math.abs(num));
					result = result.substring(0,result.length() - 2) + "XX";
					result = result +"-";
				}else{
					result =  numberFormat.format(num);
					result = result.substring(0,result.length() - 2) + "XX ";
				}
			}else {
				if(num< 0){
					result = numberFormat.format( Math.abs(num));
					result = result +"-";
				}else{
					result =  numberFormat.format(num) +" ";
				}
			}
			
			if(showFoodstamp){
				fsTag = "F";
			}
			
			if(showTax){
				taxTag = "T";
			}
			result = GlobalInfo.getInstance().CurrencySign + result + fsTag + taxTag;
			return result;
		}
		
//		public static String fixPrintPriceXX(double num,boolean showFoodstamp,boolean showTax) {
//			String result = "";
//			String fsTag = " ";
//			String taxTag = " ";
//			NumberFormat numberFormat = NumberFormat.getNumberInstance();
//			numberFormat.setMinimumFractionDigits(2);
//			numberFormat.setMaximumFractionDigits(2);
//			
//			
//			if(num< 0){
//				result = numberFormat.format( Math.abs(num));
//				result = result.substring(0,result.length() - 2) + "XX";
//				result = result +"-";
//			}else{
//				result =  numberFormat.format(num);
//				result = result.substring(0,result.length() - 2) + "XX ";
//			}
//			
//			if(showFoodstamp){
//				fsTag = "F";
//			}
//			
//			if(showTax){
//				taxTag = "T";
//			}
//			
//			result = GlobalInfo.getInstance().CurrencySign + result + fsTag + taxTag;
//			
//			return result;
//		}
		
		public static String fixPriceForScreen(double num) {
			String result = "";
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setMinimumFractionDigits(2);
			numberFormat.setMaximumFractionDigits(2);
			result = numberFormat.format(num);
			return GlobalInfo.getInstance().CurrencySign + result;
		}
		

		public static Context getShareContext(Context context) {
			
			Context sharedContext = null;
			try {
				sharedContext = context.createPackageContext(MoleQConstants.DB_PACKAGE_NAME,
						Context.CONTEXT_IGNORE_SECURITY);
				if (sharedContext == null) {
					return context;
				}
			} catch (Exception e) {
				String error = e.getMessage();
				return context;
			}
					
			return sharedContext;
		}
		
		public static Context getShareItyContext(Context context) {
			
			 Context sharedContext = null;
			 try {
				 sharedContext =
				 context.createPackageContext(MoleQConstants.ITYDB_PACKAGE_NAME,
				 Context.CONTEXT_IGNORE_SECURITY);
				 if (sharedContext == null) { 
				 return context;
				 }
			 } catch (Exception e) {
				 String error = e.getMessage();
				 return context;
			 }
			
			 return sharedContext;
		}
		

		public static String getTransType(TransactionType type){
			String trnType = "";
			if (type == TransactionType.SALE) {
				trnType = "S";
			}else if (type == TransactionType.RETURN) {
				trnType = "R";
			}else if (type == TransactionType.NOSALE) {
				trnType = "N";
			}else if (type == TransactionType.AUTH) {
				trnType = "A";
			}else if (type == TransactionType.VOID) {
				trnType = "V";
			}else if (type == TransactionType.WIC) {
				trnType = "W";
			}else if (type == TransactionType.CVV) {
				trnType = "C";
			}
			return trnType;
		}
		
		public static double fixPointToString(String num){
			double result = 0;
			if (num == null || num.trim().equals("")) {
				result = 0;
			}else if (num.startsWith(".") && num.length() == 1) {
				result = 0;
			}else {
				result = Double.valueOf(num);
			}
			return result;
		}
		
		
		public static boolean checkIp(String ip){
			boolean check = false;
			if (ip == null || ip.equals("")) {
				return check;
			}
			
			Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
			Matcher matcher = pattern.matcher(ip);
			check = matcher.matches();
			return check;
		}

		public static String fixNumberIncludeCurrencySign(double amt) {
			
			return GlobalInfo.getInstance().CurrencySign + fixNumber(amt);
		}
		
		public static int getDecimalPoint(String weightUnit) {
			int point = 2;
	
			if (weightUnit.equals("oz")) {
				point = 1;
			} else if (weightUnit.equals("lb")) {
				point = 2;
			} else if (weightUnit.equals("kg")) {
				point = 3;
			}
			return point;
		}
		
		 public static String frontCompWithZore(int sourceDate,int formatLength)  
		 {  
		  /* 
		   * 0 指前面补充零 
		   * formatLength 字符总长度为 formatLength 
		   * d 代表为正数。 
		   */  
		  String newString = String.format("%0"+formatLength+"d", sourceDate);  
		  return  newString;  
		 }
		 
		 public static String getSenderString(String title){
			 
			 String result = title+" "+frontCompWithZore(GlobalInfo.getInstance().stationNo, 2)
						+"-"+String.valueOf(TransactionManager.getInstance().TransactionNo)+" "+fixDateTime2(getNow2(), "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss");
			return result;
		 
		 }
		 
	public static ArrayList<String> parseSqlRecord(String str) {
		ArrayList<String> newStrings = new ArrayList<String>();
		boolean bContinue = false;
		String[] temps = str.split(",");
		String value = "";
		int part = 0;
		for (int i = 0; i < temps.length; i++) {
			int j = 0;
			String str1 = temps[i];

			if (str1.length() > 0) {
				String first = "";
				String end = "";

				if (!bContinue) {
					first = str1.trim();
					if(!first.equals("")){
						first = first.substring(0, 1);
						
						if (first.equals("'")) {
							bContinue = true;
							part = 1;
						}
					}
				}

				if (bContinue) {

					value += str1;

					String str2 = str1.trim();
					int count = 0;
					int k = 0;
					j = str2.length();

					do {
						if (part > 1) {
							k = 0;
						} else {
							k = 1;
						}
						end = str2.substring(j - 1, j);
						if (end.equals("'")) {
							count++;
							j--;
						} else {
							j = 0;
						}

					} while (j > k);

					if (count % 2 == 1) {
						newStrings.add(value);
						value = "";
						bContinue = false;
						part = 0;
					} else {
						value += ",";
						part++;
					}
				} else {
					newStrings.add(str1);
				}
			}
		}
		return newStrings;
	}
	
	
	
	public static int getUnit() {
		int checkunit = 2;
		String unit = ServiceManager.getInstance().mPosSettingService.Find().getPs_weight_unit();
		if (unit.equals("oz")) {
			checkunit = 1;
		} else if (unit.equals("lb")) {
			checkunit = 2;
		} else if (unit.equals("kg")) {
			checkunit = 3;
		}
		return checkunit;
	}
	
	public static int getPaymentTypeID(PaymentType paymentType){
		int funcID = 0;
		if (paymentType == PaymentType.CASH) {
			funcID = FreeFuncRpt.FF_CASH;
		}else if (paymentType == PaymentType.CREDIT_CARD) {
			funcID = FreeFuncRpt.FF_CREDIT_CARD;
		}else if (paymentType == PaymentType.DEBIT_CARD) {
			funcID = FreeFuncRpt.FF_DEBIT_CARD;
		}else if (paymentType == PaymentType.FOOD_STAMP) {
			funcID = FreeFuncRpt.FF_FOOD_STAMP;
		}else if (paymentType == PaymentType.CHECK) {
			funcID = FreeFuncRpt.FF_CHECK;
		}else if (paymentType == PaymentType.GIFT_CARD) {
			funcID = FreeFuncRpt.FF_GIFT_CARD;
		}
		return funcID;
	}
	
	public static PaymentType getPaymentbyId(int id){
		PaymentType result = null;
		if (id == FreeFuncRpt.FF_CASH) {
			result = PaymentType.CASH;
		}else if (id == FreeFuncRpt.FF_CREDIT_CARD) {
			result = PaymentType.CREDIT_CARD;
		}else if (id == FreeFuncRpt.FF_DEBIT_CARD) {
			result = PaymentType.DEBIT_CARD;
		}else if (id == FreeFuncRpt.FF_FOOD_STAMP) {
			result = PaymentType.FOOD_STAMP;
		}else if (id == FreeFuncRpt.FF_CHECK) {
			result = PaymentType.CHECK;
		}else if (id == FreeFuncRpt.FF_GIFT_CARD) {
			result = PaymentType.GIFT_CARD;
		}
		return result;
	}
}
